package com.car.service;

import com.car.dao.CarDao;
import com.car.dao.PurchaseRecordDao;
import com.car.dao.UserCarDao;
import com.car.dao.UserDao;
import com.car.dao.impl.CarDaoImpl;
import com.car.dao.impl.PurchaseRecordDaoImpl;
import com.car.dao.impl.UserCarDaoImpl;
import com.car.dao.impl.UserDaoImpl;
import com.car.doamin.Car;
import com.car.doamin.PurchaseRecord;
import com.car.doamin.User;
import com.car.doamin.UserCar;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Scanner;

public class UserService {
    private static final UserDao userDao = new UserDaoImpl();
    private static final CarDao carDao = new CarDaoImpl();
    private static final UserCarDao userCarDao = new UserCarDaoImpl();
    private static final PurchaseRecordDao purchaseRecordDao = new PurchaseRecordDaoImpl();
    /**
     * 用户注册
     */
    public Boolean register(Scanner scanner) throws Exception {
        System.out.printf("用户注册");
        User user = new User();

        System.out.printf("请输入注册用户名：");
        String name = scanner.next();
        user.setName(name);

        System.out.printf("请输入密码：");
        String password = scanner.next();
        user.setPassword(password);

        System.out.printf("请输入注册用户手机号码：");
        String phone = scanner.next();
        user.setPhone(phone);

        System.out.printf("请输入注册用户email：");
        String email = scanner.next();
        user.setName(email);
        //通过手机号 查询用户
        if (userDao.findByPhone(phone) != null){
            return false;
        }
        int add = userDao.add(user);
        return add == 1;
    }

    /**
     * 用户登录
     * @param scanner 键盘输入
     * @return 登录用户
     */
    public User login(Scanner scanner) throws Exception {
        System.out.printf("用户登录");
        System.out.printf("请输入您的手机号码");
        String phone = scanner.next();
        System.out.printf("请输入您的密码");
        String password = scanner.next();
        //select * from t_user where phone = ? and password = ?
       return userDao.findByPhoneAndPassword(phone,password);


    }

    public void buyCar(User user, Scanner scanner) throws Exception {
        System.out.println("购买汽车");
        //输入汽车id 寻找汽车 并且 还未消售
        System.out.printf("请输入要购买汽车的id:");
        int carId = scanner.nextInt();
        Car car = carDao.findByIdAndStatusEq0(carId);
        //如果汽车不存在 购买失败
        if (car == null){
            System.out.println("购买失败");
            return;
        }
        //修改汽车的状态 0是未销售 1是销售
        car.setStatus(1);
        carDao.update(car);
        //去新增中间表记录
        UserCar userCar = new UserCar();
        userCar.setCarId(carId);
        userCar.setUserId(user.getId());
        userCarDao.add(userCar);
        //购买记录表记录新增数据
        PurchaseRecord pr = new PurchaseRecord();
        pr.setCardId(carId);
        pr.setUserId(user.getId());
        pr.setEmployeeId(car.getEmployeeId());
        pr.setpurchaseDatetime(LocalDateTime.now());
        purchaseRecordDao.add(pr);
        System.out.println("恭喜购车成功");
    }

    public List<Car> ownCars(Integer userId) throws Exception{
        return userCarDao.ownCars(userId);
    }

    public void updateUserName(User user, Scanner scanner) throws Exception {
        System.out.println("修改用户名");
        System.out.printf("请输入新的用户名");
        String username = scanner.next();
        user.setName(username);
        userDao.update(user);
        System.out.printf("修改成功");
    }

    public void updataPassword(User user, Scanner scanner) throws Exception {
        System.out.println("修改密码");
        System.out.printf("请输入新的密码");
        String password = scanner.next();
        user.setPassword(password);
        userDao.update(user);
        System.out.printf("修改成功");
    }
}
